package Power_Asisst.main;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.http.SslCertificate.DName;
import android.util.Log;



public class UDP_Services {
	
	// import nhu
	private DatagramSocket socket = null;
	private DatagramPacket data ;
	private String Address = "10.0.2.2";
	private int port = 8282;
	
	
	Untils SVUntils = new Untils();
	//InetAddress IPAddress=null;
	String localIPAddress= SVUntils.getLocalIPAddress();
	String[] myArray= SVUntils.sliptIPToArr(localIPAddress);
	String[]ArrayOfIP = SVUntils.myArryList(myArray);
	// Gui 1 goi tin den toan bo cac client voi thong 
	// tin la IP va Port cua server
	public void sendBroadCastInfoToOneIP(String input) throws IOException
	{
		InetAddress IPAddress=null;
		
		IPAddress = InetAddress.getByName(input);
		try {
			DatagramSocket serVerSocket = new DatagramSocket(port);
			String dataToSend=SVUntils.getLocalIPAddress();
			DatagramPacket sendPacket= new DatagramPacket(dataToSend.getBytes(), dataToSend.length(),IPAddress,port);
			serVerSocket.send(sendPacket);
			serVerSocket.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			Log.e("Co loi khi BroadCastInfo", e.toString());
			e.printStackTrace();
		}
		

	}
	public void sendBroadCastInfoToAll()
	{
		
		
		try {
			for(int i=0;i<ArrayOfIP.length;i++)
			{
				sendBroadCastInfoToOneIP(ArrayOfIP[i]);
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.d("Loi Broadcast to all", e.toString());
		}
	}
	
	
	// import nhu 
	
	
	
	
}

